package com.dysm.spring.security.resource.service;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.math.BigDecimal;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class TestService {


    static String Secret = "bz7VJJ6bcsPpT6dv";
    static String accessToken = "8990b61d265f36b66f3ae0414ef0ca0a3dbfc2dfb09962c653c76bb0f146a9d217d0519103c2453a84ad4d1ed12e14ef0cb3650c260fb9c218f7058556938d53298c5580198b3f606afc7899aa20d5e6ec97afd06ed6b501fb4f6105cd0dcc3c6c839322ab9b54392477e7d8328802de54180fe99f1047ba975126332409267ae4ebacc446f9269918a5b069d4e4143343b4744aa9d9cba78ed4b3d65f41d914ac45eaa7a119bb9b8c0c97e98e3ec3cb";

    public static void main(String[] args) throws Exception {

        getTrainOrderDetailTest("DT181217014615138");
    }

    public static void getTrainOrderDetailTest(String orderNo) throws Exception {

//        "cityId": "0510",
//                "comeDate": "2023-01-05",
//                "leaveDate": "2023-01-08",
//                "minPrice":0,
//                "maxPrice":999999,
//                "radius": 10000,
//                "searchSort": 0,
//                "protocolType":0,
//                "pageSize": 10,
//                "pageIndex": 1
        JSONObject jParam = new JSONObject();
        jParam.put("cityId", "0510");
        jParam.put("leaveDate", "2023-01-08");
        jParam.put("minPrice", "0");
        jParam.put("maxPrice", "999999");
        jParam.put("comeDate", "2023-01-05");
        jParam.put("radius", "10000");
        jParam.put("protocolType", "0");
        jParam.put("searchSort", "0");
        jParam.put("pageSize", "10");
        jParam.put("pageIndex", "1");

        // 测试环境地址
        String url = "https://api.qa.dttrip.cn/openapi/api/Hotel/Search";

        String resultJson = sendApiRequest(url, jParam, false);

        System.out.println(resultJson);


    }

    public static String sendApiRequest(String url, JSONObject jParam, Boolean isEncrypt) throws Exception {

        String random = UUID.randomUUID().toString();
        String timestamp = Long.toString(new Date().getTime());

        JSONObject jObject = new JSONObject();
        jObject.put("isEncrypt", isEncrypt);
        jObject.put("random", random);
        jObject.put("timestamp", timestamp);
        jObject.put("accessToken", accessToken);
        jObject.put("param", jParam);

        String postData = jObject.toJSONString();

        System.out.println(postData);

        // 先将post提交的数据原文算出md5
        String postDataMd5 = getMd5(postData);
        // 再计算 sign
        String sign = getMd5(postDataMd5 + Secret);


        // get方式提交 sign
        String fullUrl = url + "?sign=" + sign;

        System.out.println("请求URL：" + fullUrl);
        System.out.println("请求参数：" + postData);

//        // 请求接口
//        String resultJson = postData(postData, fullUrl);
//
//        System.out.println("返回数据：" + resultJson);

//        return resultJson;
        return null;

    }


    public static String postData(String postData, String urlStr) {

        String result = "";
        BufferedReader in = null;
        try {
            String IP_AdRESS = urlStr;
            String urlNameString = IP_AdRESS;
            URL realUrl = new URL(urlNameString);
            URLConnection connection = realUrl.openConnection();
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(25000);
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestProperty("Accept-Charset", "UTF-8");
            connection.setRequestProperty("Content-Type", "application/json");
            String dataparam = postData;
            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
            out.write(dataparam);
            out.flush();
            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }

    public static String readToString(String fileName) throws IOException {
        File file = new File(fileName);
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        BufferedReader reader = new BufferedReader(new InputStreamReader(bis, "UTF-8"));
        StringBuffer result = new StringBuffer();
        while (reader.ready()) {
            result.append((char) reader.read());
        }
        reader.close();
        String s = result.toString();
        if (!s.startsWith("<")) {
            s = s.substring(1);
        }
        return s;
    }

    public static String getMd5(String plainText) {
        try {

            MessageDigest md = MessageDigest.getInstance("MD5");

            md.update(plainText.getBytes("utf-8"));

            byte b[] = md.digest();

            int i;

            StringBuffer buf = new StringBuffer("");
            for (int j = 0; j < b.length; j++) {
                i = b[j];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }

            return buf.toString();

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

}


